/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.model.template;

import offset.nodes.Constants;
import offset.nodes.client.model.TreeNodeUserObject;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;

/**
 *
 * @author Walter Lütgenau
 */
public abstract class AbstractTemplateNode extends ReferencableNode implements TreeNodeUserObject {

    public static final String TYPE_VIRTUAL_FOLDER = Constants.TYPE_VIRTUAL_FOLDER;
    public static final String TYPE_VIRTUAL_PAGE = Constants.TYPE_VIRTUAL_PAGE;
    public static final String TYPE_TEMPLATE_REFERENCE = Constants.TYPE_TEMPLATE_REFERENCE;
    public static final String TYPE_TEMPLATE = Constants.TYPE_TEMPLATE;
    private String type;
    protected String query;
    private NodeTypeDefinition nodeType;

    /** Creates a new instance of TemplateNode
     * @param type The type of the node
     */
    public AbstractTemplateNode(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public NodeTypeDefinition getNodeType() {
        return nodeType;
    }

    public void setNodeType(NodeTypeDefinition nodeType) {
        this.nodeType = nodeType;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getQuery() {
        return query;
    }

    public String getSchemaPath() {
        if ( query == null)
            return "";
        String[] components = query.split("[\\[\\]]");
        boolean inFilter = false;
        StringBuffer result = new StringBuffer();

        for (int i = 0; i < components.length; i++) {
            if (!inFilter) {
                result.append(components[i]);
                inFilter = true;
            } else {
                inFilter = false;
            }
        }

        return result.toString();
    }
}
